package com.apes.fn.scm.settle.repository;

import com.apes.fn.scm.settle.model.PlatformSettle;
import com.apes.fn.scm.settle.model.PlatformSettleRebate;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.Map;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.settle.repository
 * @ClassName: PlatformSettleRepository
 * @Author: pyb
 * @CreateTime: 2020-02-28 10:20
 * @Description: 平台供货结算接口
 * @Version: 1.0
 */
public interface PlatformSettleRepository extends CustomRepository<PlatformSettle, String> {
    /**
     * fetch data by max start date
     * @param supplierId partner id
     * @param deptId dept Id
     * @return PlatformSettle
     */
    @Query("select new map(T.reconcileBeginDate as reconcileBeginDate ,T.reconcileEndDate as reconcileEndDate )" +
            "from PlatformSettle T where T.reconcileBeginDate=" +
            "(select max(T.reconcileBeginDate) from PlatformSettle T where T.supplier.id =?2 and T.dept.id =?1 and( T.state='approve' or T.state='done') )" +
            " and T.supplier.id =?2 and T.dept.id =?1 and( T.state='approve' or T.state='done')  ")
    Map findByReconcileBeginDate(String deptId, String supplierId);
    /**
     * fetch data by  start date and partner id and dept Id
     * @param supplierId partner id
     * @param deptId dept Id
     * @param startDate start date
     * @return PlatformSettle
     */
    @Query("select new map(T.id as id )" +
            "from PlatformSettle T where T.reconcileBeginDate= ?3" +
            " and T.supplier.id =?2 and T.dept.id =?1 and( T.state='approve' or T.state='done')  ")
    Map findByDeptAndSupplierIdAndReconcileBeginDate(String deptId, String supplierId , Date startDate);
}
